﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using tetris_data;

namespace tetris2d_wpf
{
    /// <summary>
    /// TetrisBoard.xaml 的交互逻辑
    /// </summary>
    public partial class TetrisBoard : UserControl
    {
        GameData data = new GameData();

        private Ellipse[,] cubes = new Ellipse[20, 10];
        private Ellipse[,] previews = new Ellipse[4, 4];
        bool started = false;
        bool paused = false;

        public TetrisBoard()
        {
            this.InitializeComponent();
            data.Removing = () =>
            {
                Dispatcher.Invoke(() =>
                {
                    sound.Source = new Uri("media\\2.mp3", UriKind.Relative);
                    sound.Play();
                });
            };
        }

        private void AutoDown()
        {
            Render();

            Task.Run(() =>
            {
                data.MoveDown();
                Thread.Sleep(data.Ticks);
                if (!data.IsFull() && !paused)
                    AutoDown();
            });
        }
        private void Render()
        {

            var datas = data.BoardData;

            for (int row = 0; row < 20; row++)
            {
                for (int col = 0; col < 10; col++)
                {
                    var cell = datas[row, col];
                    Ellipse cube = cubes[row, col];
                    if (!cell.Visable)
                    {
                        Dispatcher.Invoke(() =>
                        {
                            cube.Fill = new SolidColorBrush(Colors.LightCyan);
                        });
                    }
                    else
                    {
                        Dispatcher.Invoke(() =>
                        {
                            var c = cell.Color;
                            cube.Fill = new RadialGradientBrush(Colors.White, Color.FromRgb(c.R, c.G, c.B));
                        });
                    }
                }
            }

            if (data.Current == null)
                return;
            var arr = data.Current.Data;

            int sh = data.Current.Height;
            int sw = data.Current.Width;

            for (int row = 0; row < sh; row++)
            {
                for (var col = 0; col < sw; col++)
                {
                    if (arr[row, col])
                    {
                        var cube = cubes[data.Current.Top + row, data.Current.Left + col];
                        var c = data.Current.Color;
                        Dispatcher.Invoke(() =>
                        {
                            cube.Fill = new RadialGradientBrush(Colors.White, Color.FromRgb(c.R, c.G, c.B));
                        });
                    }

                }

                for (int i = 0; i < 4; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        var c = data.Next.Color;
                        var color = data.Next.Data[i, j] ? Color.FromRgb(c.R, c.G, c.B) : Colors.LightCyan;
                        Dispatcher.Invoke(() =>
                        {
                            SolidColorBrush brush = new SolidColorBrush(color);
                            previews[i, j].Fill = brush;
                        });
                    }
                }
            }

            Dispatcher.Invoke(() =>
            {
                scoreText.Text = data.Score.ToString();
                levelText.Text = data.Level.ToString();
                rowsText.Text = data.Removed.ToString();
                if (data.IsFull())
                {
                    started = false;

                    MessageBox.Show("Game Over!");
                }
            });

        }
        public void ProcessKey(Key key)
        {
            sound.Source = new Uri("media\\1.mp3", UriKind.Relative);
            sound.Play();
            if (started && !paused)
            {
                if (key == Key.Left) { data.MoveLeft(); }
                if (key == Key.Right) { data.MoveRight(); }
                if (key == Key.Space) { data.Drop(); }
                if (key == Key.Up) { data.ChangeDirection(); }
                if (key == Key.Down) { data.MoveQuickly(); }
            }
            Render();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

            for (var col = 0; col <= 10; col++)
            {
                Line line = new Line();
                line.X1 = col * 30;
                line.X2 = col * 30;
                line.Y1 = 0;
                line.Y2 = 600;
                line.Stroke = new SolidColorBrush(Color.FromArgb(0xFF, 0, 0, 1));
                grid.Children.Add(line);
            }

            for (var row = 0; row <= 20; row++)
            {
                Line line = new Line();
                line.X1 = 0;
                line.X2 = 300;
                line.Y1 = row * 30;
                line.Y2 = row * 30;
                line.Stroke = new SolidColorBrush(Color.FromArgb(0xFF, 0, 0, 1));
                grid.Children.Add(line);
            }

            for (int row = 0; row < 4; row++)
            {
                for (int col = 0; col < 4; col++)
                {
                    Ellipse cube = new Ellipse();
                    cube.Width = 22;
                    cube.Height = 22;
                    cube.HorizontalAlignment = HorizontalAlignment.Left;
                    cube.VerticalAlignment = VerticalAlignment.Top;

                    var m = cube.Margin;
                    cube.Margin = new Thickness(m.Left + col * 25 + 3, m.Top + row * 25 + 3, m.Right, m.Bottom);
                    previews[row, col] = cube;
                    next.Children.Add(cube);
                }
            }
            for (int row = 0; row < 20; row++)
            {
                for (int col = 0; col < 10; col++)
                {
                    Ellipse cube = new Ellipse();
                    cube.Width = 24;
                    cube.Height = 24;
                    cube.HorizontalAlignment = HorizontalAlignment.Left;
                    cube.VerticalAlignment = VerticalAlignment.Top;
                    cube.Fill = new SolidColorBrush(Colors.LightCyan);
                    var m = cube.Margin;
                    cube.Margin = new Thickness(m.Left + col * 30 + 3, m.Top + row * 30 + 3, m.Right, m.Bottom);
                    cubes[row, col] = cube;
                    grid.Children.Add(cube);
                }
            }
        }

        public void Start()
        {
            sound.Source = new Uri("media\\ding.wav", UriKind.Relative);
            sound.Play();
            started = true;
            data.RandomShape();
            Render();

            AutoDown();
        }

        public void Stop()
        {
            started = false;
        }

        public void Pause()
        {
            if (started)
            {
                paused = !paused;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            scoreText.Text = "0";
            rowsText.Text = "0";
            levelText.Text = "0";
            Start();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Pause();
        }

    }
}
